package lumis.portal.serviceinstance.serialization;

import java.util.Date;

import lumis.portal.PortalException;
import lumis.portal.authentication.SessionConfig;
import lumis.portal.manager.ManagerFactory;
import lumis.portal.progress.ProgressConfig.Type;
import lumis.portal.serialization.BaseSerializationContext;
import lumis.portal.serialization.SerializationSettings;
import lumis.portal.serialization.SerializationStringResource;
import lumis.portal.serialization.persistency.IDirectory;
import lumis.portal.serialization.persistency.sub.SubDirectory;
import lumis.portal.transaction.ITransaction;

/**
 * Base Context class for service instances serialization and deserialization
 *
 * @version $Revision: 9947 $ $Date: 2008-12-04 13:35:12 -0200 (Thu, 04 Dec 2008) $
 * @since 4.0.8
 */
public abstract class ServiceInstanceBaseSerializationContext
{
	protected String serviceInstanceId;
	protected String serviceInstanceName;
	private final SerializationSettings serializationSettings = new SerializationSettings();
	private final BaseSerializationContext baseSerializationContext;
	public abstract SessionConfig getSessionConfig();
	public abstract ITransaction getTransaction();

	private final int INNER_PROGRESS_MESSAGE_INTERVAL_MILLIS = 1000;
	private int innerProgressMax;
	private int innerProgressCount;
	private long lastInnerProgressSentTime = new Date().getTime();

	public ServiceInstanceBaseSerializationContext(final BaseSerializationContext baseSerializationContext)
	{
		super();
		this.baseSerializationContext = baseSerializationContext;
	}
	
	public void setServiceInstanceId(String serviceInstanceId)
	{
		this.serviceInstanceId = serviceInstanceId;
	}
	public String getServiceInstanceId()
	{
		return serviceInstanceId;
	}

	public void setServiceInstanceName(String serviceInstanceName)
	{
		this.serviceInstanceName = serviceInstanceName;
	}
	public String getServiceInstanceName()
	{
		return serviceInstanceName;
	}

	public SerializationSettings getSerializationSettings()
	{
		return serializationSettings;
	}
	public void logDebug(String msg)
	{
		baseSerializationContext.logDebug(msg);
	}
	public void logDebug(String msg, Object o)
	{
		baseSerializationContext.logDebug(msg, o);
	}

	public void logDebug(String msg, Throwable t)
	{
		baseSerializationContext.logDebug(msg, t);
	}
	
	public String getProcessId()
	{
		return baseSerializationContext.getProcessId();
	}
	
	public boolean getIncludeContent()
	{
		return baseSerializationContext.getIncludeContent();
	}

	public IDirectory getDataDirectory()
	{
		return new SubDirectory(baseSerializationContext.getDirectory(), "serviceInstancesData/" + serviceInstanceId);
	}

	public IDirectory getStructureDirectory()
	{
		return new SubDirectory(baseSerializationContext.getDirectory(), "serviceInstances/" + serviceInstanceId);
	}
	public void initializeInnerProgressBar(int innerProgressMax, String text) throws PortalException
	{
		this.innerProgressMax = innerProgressMax;
		this.innerProgressCount = 0;
		if (this.innerProgressMax > 0)
			baseSerializationContext.sendProgressNotification(text, Type.INFO, getServiceInstanceId());
	}

	public void updateInnerProgressBar(int increment, String text) throws PortalException
	{
		if (this.innerProgressMax > 0)
		{
			this.innerProgressCount += increment;
			long currentTime = new Date().getTime();
			if (currentTime - lastInnerProgressSentTime >= INNER_PROGRESS_MESSAGE_INTERVAL_MILLIS)
			{
				updateInnerProgress(text, false);
				lastInnerProgressSentTime = currentTime;
			}
		}
	}

	public void finalizeInnerProgressBar(String text) throws PortalException
	{
		if (this.innerProgressMax > 0)
			updateInnerProgress(text, true);
	}
	
	private void updateInnerProgress(String text, boolean finalize) throws PortalException
	{
		text = ManagerFactory.getLocalizationManager().localize(text, new SerializationStringResource());
		String updateScript;
		if (finalize)
			updateScript = "finalizeMessageProgress(" + (text == null ? "null" : "\"" + text.replaceAll("\\\"", "\\\\\"") + "\"") + ",\"" + getServiceInstanceId() + "\");";
		else
			updateScript = "updateMessageProgress(" + this.innerProgressMax + "," + this.innerProgressCount + "," + (text == null ? "null" : "\"" + text + "\"") + ",\"" + getServiceInstanceId() + "\");";
		baseSerializationContext.sendProgressNotification(updateScript, Type.SCRIPT, null);
		baseSerializationContext.updateProgressBar(0, 0, null);
	}	
}
